
var loginLayer = cc.Layer.extend({
    sprite:null,
    ctor:function () {
        //////////////////////////////
        // 1. super init first
        this._super();

        /////////////////////////////
        // 2. add a menu item with "X" image, which is clicked to quit the program
        //    you may modify it.
        // ask the window size
        var size = cc.winSize;

        var mainscene = ccs.load(res.Login_json);
        this.addChild(mainscene.node);

        var statusText = mainscene.node.getChildByName('status');
        var loginButton = mainscene.node.getChildByName('Button_1');
        var nameText = mainscene.node.getChildByName('TextField_1');
        var pswText = mainscene.node.getChildByName('TextField_1_Copy');
        
        pomeloConnect(function(e,data){
        	var status = '连接中...';
        	if(!e/* && data && data.code===0 && data.data*/){
        		status = '正常';
        		statusText.setColor(cc.color(69,139,00));
        	}else{
        		status = '失败';
        		statusText.setColor(cc.color(238,00,00));
        		gg(e);
        		gg(data);
        	}
        	statusText.setString(status);
        });
        
        loginButton.addTouchEventListener(function(sender, type) {
        	if(2 === type){
        		var name = nameText.getString();
        		var psw = pswText.getString();
        		login(name,psw,function(e,data){
        			gg(name);
        			gg(psw);
        			if(!e){
        				if(data && 0 === data.code){
        					cc.director.replaceScene(new MainScene());
        				}
        			}
        		});
        	}
        });
        
        // add a "close" icon to exit the progress. it's an autorelease object
        var closeItem = cc.MenuItemImage.create(
        		res.CloseNormal_png,
        		res.CloseSelected_png,
        		function () {
        		},this);
        closeItem.attr({
        	x: size.width - 20,
        	y: 20,
        	anchorX: 0.5,
        	anchorY: 0.5
        });
        var menu = cc.Menu.create(closeItem);
        menu.x = 0;
        menu.y = 0;
        this.addChild(menu, 1);
        
        return true;
    }
});

var LoginScene = cc.Scene.extend({
    onEnter:function () {
        this._super();
        var layer = new loginLayer();
        this.addChild(layer);
    }
});

var pomelo = window.pomelo;
var uid = "uid";
var rid = "rid";
var name = "tweyseo";
var pomeloConnect = function(cb) {

	pomelo.init({
		host: "127.0.0.1",
		port: 3010,
		log: true
	}, function(data) {
		gg(data);
		cb(null,data);
	});
}

function login(name,psw,cb){
	var route = "connector.connectorHandler.login";
	pomelo.request(route, {
		name: name,
		psw: psw
	}, function(data) {
		cb(null,data);
	});
}

function chatSend() {
	var route = "userstate.userstateHandler.getUserInfo";
	var target = "*";
	var msg = "msg"
		pomelo.request(route, {params : {name: name}}, function(data) {
//				cc.log(JSON.stringify(data));
			cb(null,data);
		});
}

function gg(obj){ 
//	var description = ""; 
//	for(var i in obj){   
//		var property=obj[i];   
//		description+=i+" = "+property+"\n";
//	}   
	cc.log("=======",JSON.stringify(obj, null, 4));
} 